package com.buaa.singlethreadfileload;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.buaa.singlethreadfileload.bean.FileInfo;
import com.buaa.singlethreadfileload.service.DownLoadService;

public class MainActivity extends AppCompatActivity {
    private TextView mFileName;
    private SeekBar seekBar;
    private Button downLoad;
    private Button pause;
    public static MainActivity mMainActivity;
    private boolean btnFlag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化控件
        mFileName = (TextView) findViewById(R.id.mFileName);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        downLoad = (Button) findViewById(R.id.downLoad);
        pause = (Button) findViewById(R.id.pause);
        seekBar.setMax(95);

        //初始化文件信息对象
        final FileInfo fileInfo = new FileInfo("imooc.apk", 0, 0, 0, "http://www.imooc.com/mobile/imooc.apk");

        mFileName.setText(fileInfo.getFileName());
        //添加事件监听器
            downLoad.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //通过Intent传递参数给service
                    Intent intent = new Intent(MainActivity.this, DownLoadService.class);
                    intent.setAction(DownLoadService.ACTION_START);
                    intent.putExtra("fileInfo", fileInfo);
                    startService(intent);
                    downLoad.setEnabled(false);
                }
            });

        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //通过Intent传递参数给service
                Intent intent = new Intent(MainActivity.this, DownLoadService.class);
                intent.setAction(DownLoadService.ACTION_STOP);
                intent.putExtra("fileInfo", fileInfo);
                startService(intent);
                downLoad.setEnabled(true);

            }
        });

        //注册广播接收器
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(DownLoadService.ACTION_UPDATE);
        registerReceiver(mReceiver, intentFilter);
        mMainActivity = this;
    }

    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

    //更新UI的广播接收器
    BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (DownLoadService.ACTION_UPDATE.equals(intent.getAction())) {
                int finished = intent.getIntExtra("finished", 0);
                seekBar.setProgress(finished);
            }
        }
    };

    //监听返回键
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode && downLoad != null) {
            pause.performClick();//模拟按下暂停按钮
        }
        return super.onKeyUp(keyCode, event);
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(mMainActivity, "下载完成", Toast.LENGTH_SHORT).show();
        }
    };
}
